/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doc2db.interfaces.action.InsUpd;

import doc2db.interfaces.action.*;
import doc2db.DbManager.DbSelects;
import doc2db.Keey;
import doc2db.UserProp;
import doc2db.interfaces.mainWindow.MyJEditorPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;

/**
 *
 * @author yura
 */
public class UpdateAction extends CreateDialog implements ActionListener{

    public UpdateAction(JEditorPane __p, JEditorPane t2) {
        super(__p, t2);
    }

    @Override
    public void actionPerformed(ActionEvent even) {
        if (!Keey.isConnected) {
            return;
        }
        MyJEditorPane mP = (MyJEditorPane) super.p;
        if (mP.isInTable) {
            super.res = mP.tableName;
        }
        if (res.equals("__NULL")) {
            this.show();
        }
        if (res.equals("__NULL")) {
            return;
        }

        final DbClientDialogUpdata dialogUpdata = new DbClientDialogUpdata(null, String.valueOf(res), super.e);
        dialogUpdata.okButtonAddAction(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                String s1 = dialogUpdata.getSET();
                String s2 = dialogUpdata.getWHERE();
                String str = "UPDATE \"" + dialogUpdata.getTableName() + "\" "
                        + s1 + "  " + s2 + " ;";
                System.out.println(str);
                if(s1.equals("SET ") || s2.equals("WHERE ")){
                    JOptionPane.showMessageDialog(null, "запрос не имеет смысла",
                            "сообщение", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                DbSelects query;
                try {
                    UserProp up = new UserProp();
                    query = new DbSelects(up.getURL(), up.getUserProp());
                    if (query.sendQueryUpdate(str)) {
                        JOptionPane.showMessageDialog(null, "запрос выполнен",
                                "сообщение", JOptionPane.INFORMATION_MESSAGE);
                    }else{
                        JOptionPane.showMessageDialog(null, "запрос НЕ выполнен",
                                "ошибка", JOptionPane.ERROR_MESSAGE);
                    }
                } catch (SQLException ex) {
                        JOptionPane.showMessageDialog(null, "" + ex,
                                "ошибка", JOptionPane.ERROR_MESSAGE);
                } catch (ClassNotFoundException ex) {
                        JOptionPane.showMessageDialog(null, "" + ex,
                                "ошибка", JOptionPane.ERROR_MESSAGE);
                }
                e.setText(e.getText() + "\n" + str);
            }
        });
        res = "__NULL";
        dialogUpdata.setVisible(true);
    }

}

